package tn.redmine.tests;

import java.util.HashMap;
import java.util.Map;

import tn.redmine.data_models.User;
import tn.redmine.pages.HomePage;
import tn.redmine.pages.ReportsPage;
import core.Pilot;
import core.base.TestObjectBase;

/**
 * Class contains test methods for Index page
 */
public class RedmineTests extends TestObjectBase
{
	String hoursInMonth = "160";
	String period = "lm";

	public RedmineTests(Pilot p_manager)
	{
		super(p_manager);
	}

	/**
	 * Report test
	 */
	public void reportManualAction(User p_user)
	{
		String fileName = "2015-02-" + p_user.getLastName() + ".xls";
		// //manager.cronos().getCurrentDate("yyyy-MM-") + p_user.getLastName() + ".xls";
		String filePath = "d:\\projects\\pilotqa\\manual\\" + fileName;

		try
		{
			HomePage.openReportsDetailed(p_user, period);

			pilot.excel().create(filePath);

			// Issues
			Map<Integer, Object[]> allIssues = ReportsPage.getIssuesDetailed(p_user);
			Map<Integer, Object[]> testingIssues = ReportsPage.getTestingDetailed(allIssues);
			Map<Integer, Object[]> overtimeIssues = ReportsPage.getOvertimesDetailed(allIssues);
			Map<Integer, Object[]> otherIssues = ReportsPage.getOthersDetailed(allIssues);

			writeMonthlyReportHead(filePath,
					p_user.getLastName() + " " + p_user.getFirstName(),
					p_user.getPosition(),
					p_user.getCategory(),
					p_user.getSalary());
			writeMonthlyReportIssues(filePath, testingIssues, overtimeIssues, otherIssues, hoursInMonth);

			sendToEmail(p_user.getPosition(), p_user.getEmail(), fileName, filePath);

		}
		catch (Exception p_ex)
		{
			pilot.log().errorAndScreen("Cannot generate report\n" + p_ex.toString(), "");
		}

		pilot.tester().forErrors();
	}

	/**
	 * Report test
	 */
	public void reportAutoAction(User p_user)
	{
		String fileName = "2015-02-" + p_user.getLastName() + ".xls";
		// String fileName = pilot.date().getCurrentDate("yyyy-MM-") + p_user.getLastName() + ".xls";
		String filePath = "d:\\projects\\pilotqa\\auto\\" + fileName;

		try
		{
			HomePage.openReports(p_user, period);

			pilot.excel().create(filePath);

			// Issues
			Map<Integer, Object[]> allIssues = ReportsPage.getIssues(p_user);
			Map<Integer, Object[]> defaultIssues = ReportsPage.getDefaultIssues(allIssues);
			Map<Integer, Object[]> overtimeIssues = ReportsPage.getOvertimes(allIssues);
			Map<Integer, Object[]> otherIssues = ReportsPage.getOthers(allIssues);

			writeMonthlyReportHead(filePath,
					p_user.getLastName() + " " + p_user.getFirstName(),
					p_user.getPosition(),
					p_user.getCategory(),
					p_user.getSalary());
			writeMonthlyReportIssues(filePath, defaultIssues, overtimeIssues, otherIssues, hoursInMonth);

			sendToEmail(p_user.getPosition(), p_user.getEmail(), fileName, filePath);
		}
		catch (Exception p_ex)
		{
			pilot.log().errorAndScreen("Cannot generate report\n" + p_ex.toString(), "");
		}

		pilot.tester().forErrors();
	}

	/**
	 * Report test
	 */
	// public void reportAutoTNetworksTest(String p_projectName,
	// String period,
	// String p_memberShortName,
	// String p_memberFullName,
	// String p_memberPosition,
	// String p_memberLevel,
	// String p_memberRate,
	// String hoursInMonth,
	// String p_email)
	// {
	// String fileName = pilot.date().getCurrentDate("yyyy-MM-") + p_memberShortName + ".xls";
	// String filePath = "d:\\projects\\pilotqa\\auto\\" + fileName;
	//
	// try
	// {
	// HomePage.openReports(p_user, period);
	//
	// pilot.excel().create(filePath);
	//
	// // Issues
	// Map<Integer, Object[]> allIssues = ReportsPage.getIssues(p_memberShortName);
	// Map<Integer, Object[]> testingIssues = ReportsPage.getDefaultIssues(allIssues, p_memberShortName);
	// Map<Integer, Object[]> overtimeIssues = ReportsPage.getOvertimes(allIssues, p_memberShortName);
	// Map<Integer, Object[]> otherIssues = ReportsPage.getOthers(allIssues, p_memberShortName);
	//
	// writeMonthlyReportHead(filePath, p_memberFullName, p_memberPosition, p_memberLevel, p_memberRate);
	// writeMonthlyReportIssues(filePath, testingIssues, overtimeIssues, otherIssues, hoursInMonth);
	//
	// sendToEmail(p_memberPosition, p_email, fileName, filePath);
	//
	// }
	// catch (Exception p_ex)
	// {
	// pilot.log().errorAndScreen(p_ex.toString(), "");
	// }
	//
	// pilot.tester().forErrors();
	// }

	private void sendToEmail(String p_memberPosition, String p_email, String fileName, String filePath)
	{
		pilot.mail().send("mail.together.com",
				"igor.babar",
				"Justtell_22",
				p_email,
				"igor.babar@together.com",
				"igor.babar@together.com",
				"[Monthly report][" + p_memberPosition + "][" + fileName + "]",
				"May the Force be with you",
				fileName,
				filePath);
	}

	public void writeMonthlyReportHead(String p_fileName, String p_memberFullName, String p_memberPosition, String p_memberLevel, String p_memberSalary)
	{
		Map<Integer, Object[]> head = new HashMap<Integer, Object[]>();

		// Write full name
		head.put(0, new Object[] { "", p_memberPosition, "", "" });
		head.put(1, new Object[] { "", p_memberFullName, "", "" });
		// Write level and salary
		head.put(2, new Object[] { "", p_memberLevel, "", "" });
		head.put(3, new Object[] { "", "Salary (USD)", p_memberSalary, "" });
		pilot.excel().write(p_fileName, head, "bold", "12", "blue", "def", false, true);

		head.clear();
		head.put(0, new Object[] { "", "", "", "" });
		head.put(1, new Object[] { "", "Period:", "02.2015", "" });// manager.cronos().getCurrentDate("MM.yyyy"), "" });
		pilot.excel().write(p_fileName, head, "bold", "12", "def", "def", false, false);
	}

	public void writeMonthlyReportHeadOvertime(String p_fileName)
	{
		// Head
		Map<Integer, Object[]> head = new HashMap<Integer, Object[]>();

		// Write head Overtimes
		head.put(0, new Object[] { "Activity", "Company", "Task name (Overtime)", "Hours", "Rate", "Comment" });
		pilot.excel().write(p_fileName, head, "bold", "0", "def", "gray", true, false);
	}

	public void writeMonthlyReportHeadOthers(String p_fileName)
	{
		// Head
		Map<Integer, Object[]> head = new HashMap<Integer, Object[]>();

		// Write head Overtimes
		head.put(0, new Object[] { "Activity", "Company", "Payed Vacations/ Unpayed Vacations/ illness", "Hours", "Rate", "Comment" });
		pilot.excel().write(p_fileName, head, "bold", "0", "def", "gray", true, false);
	}

	public void writeMonthlyReportHeadIssues(String p_fileName)
	{
		// Head
		Map<Integer, Object[]> head = new HashMap<Integer, Object[]>();

		// Write head
		head.put(0, new Object[] { "Activity", "Company", "Task name", "Hours", "Rate", "Comment" });
		pilot.excel().write(p_fileName, head, "bold", "0", "def", "gray", true, false);
	}

	public void writeMonthlyReportIssues(String p_fileName,
			Map<Integer, Object[]> p_testingIssues,
			Map<Integer, Object[]> p_overtimeIssues,
			Map<Integer, Object[]> p_otherIssues,
			String p_hoursInMonth)
	{
		// Formuals
		Map<Integer, String[]> formulas = new HashMap<Integer, String[]>();
		// Formuals
		Map<Integer, Object[]> issues = new HashMap<Integer, Object[]>();

		writeMonthlyReportHeadIssues(p_fileName);

		// Write testing
		pilot.excel().write(p_fileName, p_testingIssues, "def", "0", "def", "def", true, false);

		formulas.put(0, new String[] { "", "", "", "SUM(D8:D" + (p_testingIssues.size() + 7) + ")", "" });
		pilot.excel().addFormula(p_fileName, formulas, false);

		writeMonthlyReportHeadOvertime(p_fileName);

		if (p_overtimeIssues.size() > 0)
		{
			// Write Overtimes
			pilot.excel().write(p_fileName, p_overtimeIssues, "def", "0", "def", "def", true, false);
			formulas.put(0,
					new String[] { "", "", "", "SUM(D" + (p_testingIssues.size() + 10) + ":D" + (p_overtimeIssues.size() + p_testingIssues.size() + 9) + ")",
							"" });
			pilot.excel().addFormula(p_fileName, formulas, false);
		}
		else
		{
			issues.put(0, new String[] { "", "", "", "", "", "" });
			// Write Overtimes
			pilot.excel().write(p_fileName, issues, "def", "0", "def", "def", true, false);
			formulas.put(0,
					new String[] { "", "", "", "SUM(D" + (p_testingIssues.size() + 9) + ":D" + (p_testingIssues.size() + 9) + ")", "" });
			pilot.excel().addFormula(p_fileName, formulas, false);
		}

		// Write vacations, abcenses
		writeMonthlyReportHeadOthers(p_fileName);

		if (p_otherIssues.size() > 0)
		{

			pilot.excel().write(p_fileName, p_otherIssues, "def", "0", "def", "def", true, false);
			formulas.put(0,
					new String[] { "", "", "",
							"SUM(D" + (p_testingIssues.size() + p_overtimeIssues.size() + 12) + ":D"
									+ (p_overtimeIssues.size() + p_testingIssues.size() + p_otherIssues.size() + 11)
									+ ")", "" });
			pilot.excel().addFormula(p_fileName, formulas, false);
		}
		else
		{
			issues.put(0, new String[] { "", "", "", "", "", "" });
			pilot.excel().write(p_fileName, issues, "def", "0", "def", "def", true, false);
			formulas.put(0,
					new String[] { "", "", "",
							"SUM(D" + (p_testingIssues.size() + p_overtimeIssues.size() + 12) + ":D" + (p_testingIssues.size() + +p_overtimeIssues.size() + 12)
									+ ")", "" });
			pilot.excel().addFormula(p_fileName, formulas, false);
		}

		issues.put(0, new String[] { "", "Total working hours in the month:", p_hoursInMonth, "", "" });
		// Write Footer
		pilot.excel().write(p_fileName, issues, "bold", "0", "def", "def", false, false);

	}

	public void writeEventsReportHead(String p_fileName)
	{
		// Head
		Map<Integer, Object[]> head = new HashMap<Integer, Object[]>();

		// Write head
		head.put(0, new Object[] { "Name", "Date", "Time (comming)", "Time (leaving)", "Time (total)", "Comment" });
		pilot.excel().write(p_fileName, head, "bold", "0", "def", "gray", true, false);
	}

	public void writeHoursReport(String p_fileName, Map<Integer, Object[]> p_report)
	{
		// Head
		Map<Integer, Object[]> hours = new HashMap<Integer, Object[]>();

		for (int i = 0; i < p_report.size(); i++)
		{
			hours.put(0, new Object[] {
					p_report.get(i)[0],
					p_report.get(i)[1],
					p_report.get(i)[2],
					p_report.get(i)[3],
					p_report.get(i)[4],
					p_report.get(i)[5] });

			if (p_report.get(i)[5].toString().contains("Comment") || p_report.get(i)[5].toString().contains("No events"))
			{
				pilot.excel().write(p_fileName, hours, "def", "0", "def", "orange", true, false);
			}
			else
			{
				pilot.excel().write(p_fileName, hours, "def", "0", "def", "def", true, false);
			}

			hours.clear();
		}
	}
}
